<?= $this->extend('admin\_layouts\main_iframe') ?>

<?= $this->section('content') ?>
<div class="layui-fluid">
<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
              <input type="text" name="username" placeholder="" autocomplete="off" class="layui-input">
              <?=csrf_field()?>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
              <input type="text" name="mobile" placeholder="" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
              <input type="text" name="realname" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
              <select name="status">
                <option value="<?php echo COMMON_STATUS_ON;?>"><?php echo lang('common.status.'.COMMON_STATUS_ON);?></option>
                <option value="<?php echo COMMON_STATUS_OFF;?>"><?php echo lang('common.status.'.COMMON_STATUS_OFF);?></option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
          <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
        </div>
        
        <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>  
        <script type="text/html" id="buttonTpl">
          {{#  if(d.status == 1){ }}
            <button class="layui-btn layui-btn-xs">启用</button>
          {{#  } else { }}
            <button class="layui-btn layui-btn-primary layui-btn-xs">禁用</button>
          {{#  } }}
        </script>
        <script type="text/html" id="table-useradmin-admin">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          {{#  if(d.id == "<?=session()->get('id')?>"){ }}
            <a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
          {{#  } else { }}
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
          {{#  } }}
        </script>
	</div>
</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
  layui.use(['form', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table;

    // 初始加载
    table.render({
        elem: "#LAY-user-back-manage",
        url: "/admin/admintor/index",
        cols: [
            [{
                type: "checkbox",
                fixed: "left"
            }, {
                field: "id",
                width: 80,
                title: "ID",
                sort: !0
            }, {
                field: "username",
                title: "登录名"
            }, {
                field: "mobile",
                title: "手机号"
            }, {
                field: "realname",
                title: "真实姓名"
            }, {
                field: "created_at",
                title: "注册时间",
                sort: !0
            }, {
                field: "status",
                title: "状态",
                templet: "#buttonTpl",
                minWidth: 80,
                align: "center"
            }, {
                title: "操作",
                width: 150,
                align: "center",
                fixed: "right",
                toolbar: "#table-useradmin-admin"
            }]
        ],
        // page:!0,limit:10,limits:[10,15,20,25,30],
        text: "对不起，加载出现异常！"
	});
	
    //监听搜索
    form.on('submit(LAY-user-back-search)', function(data){
		var field = data.field;
      	//执行重载
      	table.reload('LAY-user-back-manage', {
        	where: field
      	});
    });

    // 编辑和删除
    table.on("tool(LAY-user-back-manage)", function(e) {
    	var mydata = e.data;
        if ("del" === e.event){
            layer.confirm("确定删除此管理员？", function(index) {
            	// 执行删除
              	$.ajax({
            		url : '/admin/admintor/batchDelete',
		          	type : 'post',
		          	dataType : 'json',
		          	data : {'data':[e.data],'csrf_token':$("input[name=csrf_token]").val()},
		          	success : function(res){
	                	if(res.code==0){
		                	e.del();
	                    	layer.close(index); //关闭弹层
		                	layer.msg('已删除');
	                 	}else{
	                     	layer.open({
	                 	  		content: res.msg
	                 		});
	                	}
					}
              	});
            });
        }else if ("edit" === e.event) {
            $(e.tr);
            layer.open({
                type: 2,
                title: "编辑管理员",
                content: "/admin/admintor/edit_form?id="+mydata.id,
                area: ["420px", "420px"],
                btn: ["确定", "取消"],
                yes: function(e, t) {
                    var l = window["layui-layer-iframe" + e],
                        r = "LAY-user-back-submit",
                        n = t.find("iframe").contents().find("#" + r);
                    l.layui.form.on("submit(" + r + ")", function(t) {
                        //提交 Ajax 成功后，静态更新表格中的数据
                      	$.ajax({
                    		url : '/admin/admintor/edit',
        		          	type : 'post',
        		          	dataType : 'json',
        		          	data : t.field,
        		          	success : function(res){
        	                	if(res.code==0){
        	                		table.reload("LAY-user-back-manage"); //数据刷新
        	                    	layer.close(e); //关闭弹层
        	                 	}else{
        	                     	layer.open({
        	                 	  		content: res.msg
        	                 		});
        	                	}
    						}
                      	});
                    }), n.trigger("click")
                },
                success: function(e, t) {}
            })
        }
    });
  
    //事件
	var active = {
    	// 批量删除管理员
		batchdel: function(){
        	var checkStatus = table.checkStatus('LAY-user-back-manage')
        		,checkData = checkStatus.data; //得到选中的数据

            if(checkData.length === 0){
              return layer.msg('请选择数据');
            }
          	layer.confirm('确定删除吗？', function(index) {
        		//console.log(checkData);
                //执行 Ajax 后重载
        		$.ajax({
            		url : '/admin/admintor/batchDelete',
		          	type : 'post',
		          	dataType : 'json',
		          	data : {'data':checkData,'csrf_token':$("input[name=csrf_token]").val()},
		          	success : function(res){
	                	if(res.code==0){
	                		table.reload('LAY-user-back-manage');
        	                layer.msg('已删除');
	                 	}else{
	                     	layer.open({
	                 	  		content: res.msg
	                 		});
	                	}
					}
              	});
      		});
		}
		// 添加管理员 
      	,add: function(){
        	layer.open({
				type: 2
          		,title: '添加管理员'
          		,content: '/admin/admintor/edit_form'
          		,area: ['420px', '420px']
          		,btn: ['确定', '取消']
				,yes: function(index, layero){
          			var iframeWindow = window['layui-layer-iframe'+ index]
            			,submit = layero.find('iframe').contents().find('#LAY-user-back-submit')
            			,submit_form = layero.find('iframe').contents().find('#edit_form');
                    //监听提交
          			iframeWindow.layui.form.on('submit(LAY-user-back-submit)', function(data){
                    	var field = data.field; //获取提交的字段
                      	// console.log(field);
                      	//提交 Ajax 成功后，静态更新表格中的数据
                      	$.ajax({
                    		url : '/admin/admintor/edit',
        		          	type : 'post',
        		          	dataType : 'json',
        		          	data : field,
        		          	success : function(res){
        	                	if(res.code==0){
        	                		table.reload('LAY-user-back-manage'); //数据刷新
        	                    	layer.close(index); //关闭弹层
        	                 	}else{
        	                     	layer.open({
        	                 	  		content: res.msg
        	                 		});
        	                	}
    						}
                      	});
                    });
                    submit.trigger('click');
				}
			}); 
		}
    } 
    // 批量删除，添加事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
});
</script>
<?= $this->endSection() ?>